package com.toune.villager.activity

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.toune.dltools.http.GsonBinder
import com.toune.dltools.ui.DLBaseActivity
import com.toune.villager.R
import com.toune.villager.adapter.CatalogAdapter
import com.toune.villager.model.BookModel
import com.zia.easybookmodule.bean.Book
import com.zia.easybookmodule.bean.Catalog
import kotlinx.android.synthetic.main.activity_catalog.*

class CatalogActivity : DLBaseActivity<BookModel>() {
    override val layout: Int
        get() = R.layout.activity_catalog
    override val titleStr: String?
        get() = "目录"

    lateinit var book: Book
    override fun init(savedInstanceState: Bundle?) {
        val string = intent.extras!!.getString("book")
        if (string!!.isNotEmpty()) {
            book = GsonBinder.toObj(string, Book::class.java)!!
            mViewModel.getCatalog(book)
            mViewModel.mCatalogs.observe(this, {
                setCatalogAdapter(it)
            })
        }
    }

    var adapter: CatalogAdapter? = null
    private fun setCatalogAdapter(it: List<Catalog>?) {
        if (adapter == null) {
            adapter = CatalogAdapter(R.layout.adapter_catalog, it as MutableList<Catalog>)
            mRclView.layoutManager = LinearLayoutManager(context)
            mRclView.adapter = adapter
            adapter!!.setOnItemClickListener { adapter, view, position ->

            }
            adapter!!.setEmptyView(R.layout.layout_empty)
        } else {
            adapter!!.notifyDataSetChanged()
        }
    }

    override fun initEventAndData() {
    }
}